package com.xiaowen.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaowen.project.dto.ProjectAddReqDTO;
import com.xiaowen.project.dto.ProjectUpdateReqDTO;
import com.xiaowen.project.entity.Project;
import com.xiaowen.project.service.ProjectService;
import com.xiaowen.tool.UserContext;
import com.xiaowen.tool.dto.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 项目表
 *
 * @author yf -wenhao
 * @since 2024 -09-18
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 获取列表
     *
     * @param current 当前页
     * @param size    每页个数
     * @return the result
     * @author wh
     * @date 2024 -09-18 14:17:06
     */
    @GetMapping(value = "/list")
    public Result<Page<Project>> list(@RequestParam(defaultValue = "1") long current,
                                      @RequestParam(defaultValue = "50") long size,
                                      @RequestParam(required = false) String projectName) {
        Integer userId = UserContext.getUser().getId();
        Project project = new Project();
        project.setDeleteFlag(false);
        project.setUserId(userId);
        if (StringUtils.isNotEmpty(projectName)) {
            project.setProjectName(projectName);
        }
        Page<Project> projectPage = projectService.selectByPage(current, size, project);
        return new Result<Page<Project>>().ok(projectPage);
    }


    /**
     * 删除
     * @param id the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/del/{id}")
    public Result delete(@PathVariable Integer id) {
        projectService.deleteById(id);
        return new Result();
    }


    /**
     * 查询详情
     * @param id the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/{id}")
    public Result<Project> selectOne(@PathVariable Integer id) {
        return new Result<Project>().ok(projectService.getById(id));
    }


    /**
     * 新增
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:04
     */
    @PostMapping
    public Result<Integer> add(@RequestBody ProjectAddReqDTO reqDTO) {
        Project project = new Project();
        BeanUtils.copyProperties(reqDTO, project);
        projectService.add(project);
        return new Result<Integer>().ok(project.getId());
    }



    /**
     * 编辑
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:04
     */
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody ProjectUpdateReqDTO reqDTO) {
        Project project = new Project();
        BeanUtils.copyProperties(reqDTO, project);
        boolean b = projectService.updateById(project);
        return new Result<Integer>().ok(project.getId());
    }

}
